package com.ruoyi.hotel.controller;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Arrays;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.hotel.domain.*;
import com.ruoyi.hotel.domain.bo.HmBillBo;
import com.ruoyi.hotel.domain.bo.HmCheckinBo;
import com.ruoyi.hotel.domain.vo.HmBillVo;
import com.ruoyi.hotel.domain.vo.HmCheckinVo;
import com.ruoyi.hotel.mapper.*;
import com.ruoyi.hotel.service.IHmCheckinService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hotel.service.IHmBillService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 账单操作
 *
 * @date 2023-10-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/hotel/bill")
public class HmBillController extends BaseController {

    private final IHmBillService iHmBillService;
    private final IHmCheckinService iHmCheckinService;


    /**
     * 查询【请填写功能名称】列表
     */
    @SaCheckRole("manager")
    @GetMapping("/list")
    public TableDataInfo<HmBillVo> list(HmBillBo bo, PageQuery pageQuery) {
        return iHmBillService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HmBillBo bo, HttpServletResponse response) {
        bo.setCustomerId(getUserId());
        List<HmBillVo> list = iHmBillService.queryList(bo);
        ExcelUtil.exportExcel(list, "我的账单", HmBillVo.class, response);
    }

    /**
     * 获取【请填写功能名称】详细信息
     *
     * @param billId 主键
     */
    @SaCheckPermission("hotel:bill:query")
    @GetMapping("/{billId}")
    public R<HmBillVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long billId) {
        return R.ok(iHmBillService.queryById(billId));
    }

    /**
     * 新增【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HmBillBo bo) {
        return toAjax(iHmBillService.insertByBo(bo));
    }

    /**
     * 修改【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HmBillBo bo) {
        return toAjax(iHmBillService.updateByBo(bo));
    }

    /**
     * 删除【请填写功能名称】
     *
     * @param billIds 主键串
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{billIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] billIds) {
        return toAjax(iHmBillService.deleteWithValidByIds(Arrays.asList(billIds), true));
    }

    /**
     * 查看个人帐单
     */
    @RequestMapping("/myBillList")
    public TableDataInfo<HmBill> myBillList(){
        Long id=getUserId();
        return TableDataInfo.build(iHmBillService.getMyBill(id));
    }

    /**
     *已入住列表，用于生成账单对象
     */
    @RequestMapping("/checkinList")
    public TableDataInfo<HmCheckinVo> checkinList(HmCheckinBo bo, PageQuery pageQuery) {
        bo.setIsDone(false);
        return iHmCheckinService.queryPageList(bo, pageQuery);
    }


    /**
     * 退房，同时生成账单
     * @param hmCheckin
     */
    @PostMapping("/checkout")
    public void checkout(@RequestBody HmCheckinBo hmCheckin){
        hmCheckin.setIsDone(true);
        iHmCheckinService.updateByBo(hmCheckin);

        iHmBillService.createBill(hmCheckin,getUserId());
    }

}
